<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    #############################################################################################
    # Programmer: นวัตพล ยศกำธร     						                        #
    # Email : golf.enfc40@gmail.com								#
    # Website : http://www.nawattapon.com							#
    #############################################################################################
    
    if ( ! function_exists('remove_dir')){
        function remove_dir($dir){
            if(is_dir($dir)){
                $dir = (substr($dir, -1) != "/")? $dir."/":$dir;
                $openDir = opendir($dir);
                while($file = readdir($openDir)){
                    if(!in_array($file, array(".", ".."))){
                        if(!is_dir($dir.$file)){
                            unlink($dir.$file);
                        }else{
                            remove_dir($dir.$file);
                        }
                    }
                }
                closedir($openDir);
                rmdir($dir);
            }
        }
    }
    
    if ( ! function_exists('img_exists')){
	function img_exists($folder , $img){
	    if($img==''){
		return FALSE;
	    }else if(file_exists($folder.'/'.$img)){
		return TRUE;
	    }else{
		return FALSE;
	    }
	}
    }
    
    if ( ! function_exists('utf8_to_tis620')){
        function utf8_to_tis620($string)
        {
            $str = $string;
            $res = "";
            for ($i = 0; $i < strlen($str); $i++) {
                if (ord($str[$i]) == 224) {
                    $unicode = ord($str[$i+2]) & 0x3F;
                    $unicode |= (ord($str[$i+1]) & 0x3F) << 6;
                    $unicode |= (ord($str[$i]) & 0x0F) << 12;
                    $res .= chr($unicode-0x0E00+0xA0);
                    $i += 2;
                } else {
                    $res .= $str[$i];
                }
            }
            return $res;
        }
    }
    
    if ( ! function_exists('substr_utf8')){
        function substr_utf8( $str, $start_p , $len_p){
            return preg_replace( '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$start_p.'}'.
            '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len_p.'}).*#s',
            '$1' , $str );
        }
    }
    
    if ( ! function_exists('alert')){
        function alert($msg) {
            echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>';
            echo "<script language = 'javascript'>";
            echo "alert('$msg')";
            echo "</script>";
        }
    }
    
    if ( ! function_exists('get_pagination')){
	function get_pagination($page=1,$row,$limit=20)
	{
	    $all_page = ceil($row/$limit);
	    $pagination = array();
	    $i = 1;
	    for($i;$i<=$all_page;$i++){
		$pagination[$i] = '';
		
		if($i==$page){
		    $pagination[$i] = 'current';
		}
	    }
	    
	    if(count($pagination)<1){
		$pagination['1'] = '';
	    }
	    
	    return $pagination;
	}
    }
    
    if ( ! function_exists('get_encryption')){
	function get_encryption($str)
	{
	    $key = 'nawattapon.yoskamtorn';
	    return sha1(md5($str).md5($str.$key).sha1($str).sha1($str.$key));
	}
    }
    
    if ( ! function_exists('generate_seo_link')){
	function generate_seo_link($input,$replace = '-',$remove_words = true,$words_array = array())
	{
	    //make it lowercase, remove punctuation, remove multiple/leading/ending spaces
	    $return = trim(ereg_replace(' +',' ',preg_replace('/[^a-zA-Z0-9\s]/','',strtolower($input))));
    
	    //remove words, if not helpful to seo
	    //i like my defaults list in remove_words(), so I wont pass that array
	    if($remove_words) { $return = remove_words($return,$replace,$words_array); }
    
	    //convert the spaces to whatever the user wants
	    //usually a dash or underscore..
	    //...then return the value.
	    return str_replace(' ',$replace,$return);
	}
    }
    
    if ( ! function_exists('remove_words')){
	/* takes an input, scrubs unnecessary words */
	function remove_words($input,$replace,$words_array = array(),$unique_words = true)
	{
	    //separate all words based on spaces
	    $input_array = explode(' ',$input);
    
	    //create the return array
	    $return = array();
    
	    //loops through words, remove bad words, keep good ones
	    foreach($input_array as $word)
	    {
		    //if it's a word we should add...
		    if(!in_array($word,$words_array) && ($unique_words ? !in_array($word,$return) : true))
		    {
			    $return[] = $word;
		    }
	    }
    
	    //return good words separated by dashes
	    return implode($replace,$return);
	}
    }
    
    if ( ! function_exists('generateSlug')){
	function generateSlug($phrase, $maxLength=1028)
	{
	    $result = strtolower($phrase);
	    
	    $result = preg_replace("/[^a-z0-9\s-]/", "", $result);
	    $result = trim(preg_replace("/[\s-]+/", " ", $result));
	    $result = trim(substr($result, 0, $maxLength));
	    $result = preg_replace("/\s/", "-", $result);
	    
	    return $result;
	}
    }
    
/* End of file devlism_helper.php */
/* Location: ./application/helpers/devlism_helper.php */